package com.imay.live.activity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.imay.base.adapter.SimpleFragmentPagerAdapter;
import com.imay.base.fragment.BaseFragment;
import com.imay.base.widget.TitleBar;
import com.imay.live.R;
import com.imay.live.adapter.DiscoveryIndicatorAdapter;
import com.imay.live.adapter.DynamicsIndicatorAdapter;
import com.imay.live.fragment.dynamics.DynamicsFragment;
import com.imay.live.heartlikeslib.byeburgernavigationview.ByeBurgerBehavior;
import com.imay.live.utils.AnimationUtil;
import com.imay.live.utils.GlideUtil;
import com.imay.live.utils.ViewUtil;
import com.imay.live.view.WebpView;
import com.imay.live.view.indicator.Indicator;
import com.imay.live.view.indicator.ScrollIndicatorView;
import com.imay.live.view.indicator.slidebar.ColorBar;
import com.imay.live.view.indicator.transition.OnTransitionTextListener;

/**
 * MainActivity主页UI部分
 * Created by Administrator on 2017/8/9 0009.
 */
public abstract class BaseMainUiActivity extends BasePermissionActivity implements AppBarLayout.OnOffsetChangedListener {

    protected boolean isStart = true;

    protected float mTitleBarHeight;
    protected LinearLayout main_rg_tab;
    protected TitleBar titleBar;
    protected View iv_bgwen;
    protected AppBarLayout mAppBarLayout;
    protected FrameLayout fl_cocos_view_container;
    protected RelativeLayout container;
    protected FrameLayout fl_main;
    protected FrameLayout frame_user_content;
    protected FrameLayout frame_user_login;
    protected FrameLayout frame_user_load;
    protected ViewPager mViewPager;
    protected View profileSysNoticeView;
    protected ImageView iv_profile_system_notice_red;
    protected SimpleFragmentPagerAdapter mViewPagerAdapter;
    protected SimpleDraweeView sdv_first_rechage;


    //标题栏滑动相关
    protected float mLastXScale = 1;
    protected float mLastScale = 1;
    protected float mLastAlpha = 1;
    protected Animator mTabAnimator;
    private int mLastVerticalOffset;
    private boolean mExpanded;
    private boolean appLayoutHide = false;


    /**
     * 动态头部
     */
    protected ScrollIndicatorView dynamcisHead;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean useDynamicTitleBar() {
        return false;
    }

    protected abstract void appLayoutIsHide(boolean hide);

    /**
     * 初始化UI
     */
    protected void initWidget() {
        main_rg_tab = $(R.id.main_rg_tab);

        mTitleBarHeight = getResources().getDimension(R.dimen.title_bar_height);
        titleBar = $(R.id.main_tb_title_bar);
        iv_bgwen = $(R.id.iv_bgwen);
        setTitleBar(titleBar);

        mAppBarLayout = $(R.id.main_abl_appbar);

        fl_cocos_view_container = $(R.id.fl_cocos_view_container);
        frame_user_content = $(R.id.frame_user_content);
        frame_user_load = $(R.id.frame_user_load);
        frame_user_login = $(R.id.frame_user_login);
        container = $(R.id.container);
        fl_main = $(R.id.fl_main);
        sdv_first_rechage = $(R.id.sdv_first_rechage);


        mViewPager = $(R.id.main_vp_pager);

        addCocosView(fl_cocos_view_container);

        profileSysNoticeView = getProfileSysNoticeView();
        titleBar.getLeftView().addView(profileSysNoticeView);


        /**
         * 头部
         */
        dynamcisHead = (ScrollIndicatorView) LayoutInflater.from(this).inflate(R.layout.view_dynamics_title, null);
        String[] tabTitles = new String[]{getString(R.string.str_dynamics_title_focus), getString(R.string.str_dynamics_title_suggest)};
        ColorBar colorBar = new ColorBar(this, Color.TRANSPARENT, (int) getResources().getDimension(R.dimen.size_3dp));
        colorBar.setWidth((int) getResources().getDimension(R.dimen.tab_ind_w));
        dynamcisHead.setScrollBar(colorBar);
        DynamicsIndicatorAdapter mIndicatorAdapter = new DynamicsIndicatorAdapter(this, getSupportFragmentManager(), tabTitles, R.layout.view_indicator_tab3);
        dynamcisHead.setAdapter(mIndicatorAdapter.getIndicatorAdapter());
        dynamcisHead.setOnTransitionListener(new Indicator.OnTransitionListener() {
            @Override
            public void onTransition(View view, int position, float selectPercent) {
                if (selectPercent == 0.0) {
                    TextView itemView1 = (TextView) dynamcisHead.getItemView(0);
                    if (itemView1 != null) {
                        itemView1.getPaint().setFakeBoldText(true);
                    }

                    TextView itemView2 = (TextView) dynamcisHead.getItemView(1);
                    if (itemView2 != null) {
                        itemView2.getPaint().setFakeBoldText(true);
                    }

                    TextView itemView = (TextView) dynamcisHead.getItemView(position);
                    if (itemView != null) {
                        itemView.getPaint().setFakeBoldText(false);
                    }
                }
            }
        });
    }

    private View getProfileSysNoticeView() {
        profileSysNoticeView = LayoutInflater.from(this).inflate(R.layout.view_system_notice, null);
        iv_profile_system_notice_red = ViewUtil.findView(profileSysNoticeView, R.id.iv_profile_system_notice_red);
        profileSysNoticeView.setVisibility(View.GONE);
        return profileSysNoticeView;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP && mLastVerticalOffset != 0) {
            mAppBarLayout.setExpanded(mExpanded);
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 开启依赖
     */
    public void openCoordBehavior() {
        if (main_rg_tab != null && mAppBarLayout != null) {
            AppBarLayout.Behavior behavior = new AppBarLayout.Behavior(this, null);
            CoordinatorLayout.LayoutParams p1 = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
            p1.setBehavior(behavior);
            ByeBurgerBehavior.from(main_rg_tab).setCanHide(true);
            ByeBurgerBehavior.from(main_rg_tab).setFirstY(main_rg_tab.getY());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (mViewPager != null) {
                    mViewPager.setNestedScrollingEnabled(true);
                }
            }
        }
    }

    /**
     * 关闭依赖
     */
    public void closeCoordBehavior() {
        if (main_rg_tab != null && mAppBarLayout != null) {
            ByeBurgerBehavior.from(main_rg_tab).setCanHide(false);
            CoordinatorLayout.LayoutParams p1 = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
            p1.setBehavior(null);
            mAppBarLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scaleTitleBar(0);
                }
            }, 100);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (mViewPager != null) {
                    mViewPager.setNestedScrollingEnabled(false);
                }
            }
        }
    }

    /**
     * 缩放titlebar
     *
     * @param verticalOffset
     */
    protected void scaleTitleBar(int verticalOffset) {
        float offsetPercent = verticalOffset / mTitleBarHeight;
        float toScale = 1 + (offsetPercent * 3 / 7F);
        float toAlpha = 1 + (offsetPercent * 10 / 7F);
        ScaleAnimation leftScaleAnimation = null;
        ScaleAnimation rightScaleAnimation = null;
        ScaleAnimation scaleAnimation = null;
        AlphaAnimation alphaAnimation = null;
        if (mLastScale != toScale) {
            leftScaleAnimation = new ScaleAnimation(mLastScale, toScale, mLastScale, toScale, Animation.RELATIVE_TO_SELF, 1F, Animation.RELATIVE_TO_PARENT, 1F);
            rightScaleAnimation = new ScaleAnimation(mLastScale, toScale, mLastScale, toScale, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_PARENT, 1F);
            scaleAnimation = new ScaleAnimation(mLastScale, toScale, mLastScale, toScale, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_PARENT, 1F);
            mLastScale = toScale;
        }
        if (mLastAlpha != toAlpha && toAlpha <= 1) {
            alphaAnimation = new AlphaAnimation(mLastAlpha, toAlpha);
            mLastAlpha = toAlpha;
        }
        if (scaleAnimation != null || alphaAnimation != null) {
            AnimationUtil.startAnimation(getTitleBar().getLeftView(), leftScaleAnimation, alphaAnimation);
            AnimationUtil.startAnimation(getTitleBar().getMiddleView(), scaleAnimation, alphaAnimation);
            AnimationUtil.startAnimation(getTitleBar().getRightView(), rightScaleAnimation, alphaAnimation);
        }
        if (mTitleBarHeight + verticalOffset == 0) {    //完全移除屏幕
            if (mAppBarLayout.getAlpha() == 1) {
                mAppBarLayout.setAlpha(0.9F);
            }
        } else {
            if (mAppBarLayout.getAlpha() != 1) {
                mAppBarLayout.setAlpha(1F);
            }
        }
    }

    /**
     * 给tab添加点击动画
     *
     * @param view
     */
    protected void startTabAnimtor(View view) {
        if (mTabAnimator == null) {
            mTabAnimator = AnimatorInflater.loadAnimator(this, R.animator.home_tab_anim);
        }
        mTabAnimator.setTarget(view);
        mTabAnimator.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAppBarLayout.addOnOffsetChangedListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAppBarLayout.removeOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset > 0) {
            return;
        }
        boolean hide = verticalOffset < 0;
        if (hide != appLayoutHide) {
            appLayoutHide = hide;
            appLayoutIsHide(hide);
        }

        mExpanded = mLastVerticalOffset < verticalOffset;
        mLastVerticalOffset = verticalOffset;
        scaleTitleBar(verticalOffset);

        int position = mViewPager.getCurrentItem();
        if (mViewPagerAdapter != null) {
            BaseFragment fragment = (BaseFragment) mViewPagerAdapter.getItem(position);
            fragment.setSpringViewEnable(verticalOffset == 0);
        }
    }

    /**
     * 修改Title的
     *
     * @param action
     */
    public void setTitleCurrentItem(int action) {
        if (action == DynamicsFragment.ACTION_SUGGEST) {
            dynamcisHead.setCurrentItem(1);
        } else if (action == DynamicsFragment.ACTION_FOCUS) {
            dynamcisHead.setCurrentItem(0);
        }
    }
}
